import {getWeChatLogin,getUserInfo,saveAvatarAndName} from '../../api/login'
import {getMemberList,getSchoolList} from '../../api/member'
Component({
  data:{
   checked:false
  },
  methods:{
    onLoad(){
    },
    // 微信登陆
    getUserProfile(e: WechatMiniprogram.ButtonGetUserInfo){
      if(!this.data.checked) return wx.Utils.toast("请先勾选协议")
      const userType = e.currentTarget.dataset.type;
  // 用户允许获取个人信息
      wx.getUserProfile({
        desc:"用于登录",
        success:(profileRes)=>{
        const userInfoSys = profileRes.userInfo;
        if (userInfoSys) {
          wx.login({
            success:res=>{
              if(res.code){
                // 服务器请求模拟
                getApp().setUserType(userType)
                getWeChatLogin({
                  code:res.code,
                  userType
                }).then(async ({data:res})=>{
                  const openId = res.openId
                 await getApp().setToken('token',res.token)
                if(res.newUser){
                // 保存用户头像跟名称
                if(res.token){
                  saveAvatarAndName({
                    avatar:userInfoSys.avatarUrl,
                    nickName:userInfoSys.nickName
                  })
                }
     
                  // 新用户跳转到信息注册页面
                  this.isNewsUesr(userType,openId,userInfoSys)
                }else{
                //  获取用户信息         
                const {data:userInfo} = await getUserInfo();                
                if(!userInfo.avatar || !userInfo.nickName){
                  saveAvatarAndName({
                  avatar:userInfo.avatar || userInfoSys.avatarUrl,
                  nickName:userInfo.nickName || userInfoSys.nickName
                })
                }
                const {phonenumber} = userInfo
                getApp().setUserInfo('userInfo',userInfo)
                if(!phonenumber) {
                  this.isNewsUesr(userType,openId,userInfoSys)
                  return
                }               
                if(userType  === "02"){
                  //获取名下成员列表
                  const {data:memberList} =  await getMemberList({userId:userInfo.userId})
                  getApp().setMemberList(memberList)
                }
              // 获取校区列表
              const {data:schoolList} =  await getSchoolList()
              getApp().setSchoolList(schoolList)
                wx.switchTab({
                  url:'/pages/index/index'
                })
                  wx.Utils.toast("登录成功！")     
                }
                }).catch(err=>{
                  wx.Utils.toast("登录失败")
                  return Promise.reject(err)
                })
              }
            }
          })
      }
        }
      })
 
  },
  // 同意协议
  agree(){
    this.setData({
      checked: !this.data.checked
    })
  },
  // 判断是否新用户或者没有填写手机号码
  isNewsUesr(userType:string,openId:string,userInfoSys:{avatarUrl:string,nickName:string}){
    switch(userType){
      case '01':
         const {avatarUrl,nickName} = userInfoSys
        wx.navigateTo({
          url:`/pages/phoneLogin/phoneLogin?openId=${openId}&avatarUrl=${avatarUrl}&nickName=${nickName}`
        })
      break;
      case '02':
    wx.navigateTo({
      url:`/pages/register/register?openId=${openId}`
    })
      break;
    }
  }
}})